package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exceptions.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FeeKeyMapper feeKeyMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private RoomInfoService roomInfoService;
    @Override
    public void customSaveOrUpdate(ApartmentSubmitVo apartmentSubmitVo) {
        //先判断是保存还是更新
        boolean isUpdate = apartmentSubmitVo.getId() != null;
        super.saveOrUpdate(apartmentSubmitVo);
        Long apartmentId = apartmentSubmitVo.getId();
        if (isUpdate){
            //删除配套的中间表
            LambdaQueryWrapper<ApartmentFacility> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ApartmentFacility::getApartmentId,apartmentId);
            apartmentFacilityService.remove(wrapper);
            //删除杂费的中间表
            LambdaQueryWrapper<ApartmentFeeValue> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(ApartmentFeeValue::getApartmentId,apartmentId);
            apartmentFeeValueService.remove(wrapper1);
            //删除标签的中间表
            LambdaQueryWrapper<ApartmentLabel> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(ApartmentLabel::getApartmentId,apartmentId);
            apartmentLabelService.remove(wrapper2);
            //删除公寓图片表
            LambdaQueryWrapper<GraphInfo> wrapper3 = new LambdaQueryWrapper<>();
            wrapper3.eq(GraphInfo::getItemId, ItemType.APARTMENT).eq(GraphInfo::getItemId,apartmentId);
            graphInfoService.remove(wrapper3);
        }
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            ArrayList<ApartmentFacility> list = new ArrayList<>(facilityInfoIds.size());

            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility build = ApartmentFacility.builder().facilityId(facilityInfoId).apartmentId(apartmentId).build();
                list.add(build);
            }
            apartmentFacilityService.saveBatch(list);
        }

        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)){
            ArrayList<ApartmentFeeValue> list = new ArrayList<>(feeValueIds.size());
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue build = ApartmentFeeValue.builder().feeValueId(feeValueId).apartmentId(apartmentId).build();
                list.add(build);
            }
            apartmentFeeValueService.saveBatch(list);
        }
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)){
            ArrayList<ApartmentLabel> list = new ArrayList<>(labelIds.size());
            for (Long labelId : labelIds) {
                ApartmentLabel build = ApartmentLabel.builder().labelId(labelId).apartmentId(apartmentId).build();
                list.add(build);
            }
            apartmentLabelService.saveBatch(list);
        }

        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            ArrayList<GraphInfo> list = new ArrayList<>(graphVoList.size());
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemId(apartmentId);
                graphInfo.setItemType(ItemType.APARTMENT);
                list.add(graphInfo);
            }
            graphInfoService.saveBatch(list);
        }
    }

    @Override
    public void customIpage(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {

        apartmentInfoMapper.customPageQuery(page,queryVo);
    }

    @Override
    public ApartmentDetailVo customGetById(Long id) {

        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);

        List<FacilityInfo> list = facilityInfoMapper.customFacilityById(id);

        List<LabelInfo> list1 = labelInfoMapper.customLabelById(id);

        List<FeeValueVo> list2 = feeValueMapper.customFeeKeyById(id);

        List<GraphVo> list3 = graphInfoMapper.customGraphById(ItemType.APARTMENT,id);


        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        apartmentDetailVo.setFacilityInfoList(list);
        apartmentDetailVo.setFeeValueVoList(list2);
        apartmentDetailVo.setLabelInfoList(list1);
        apartmentDetailVo.setGraphVoList(list3);

        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        return apartmentDetailVo;
    }

    @Override
    public void customRemoveById(Long id) {

        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId,id);
        long count = roomInfoService.count(roomInfoLambdaQueryWrapper);
        if (count > 0) {
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        removeById(id);

        LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT).eq(GraphInfo::getItemId,id);
        graphInfoService.removeById(lambdaQueryWrapper);

        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);
    }
}




